package com.king.cms.touch.action.admin;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.InstantiationException;

import com.king.cms.action.BaseDispatchAction; 
import com.king.cms.common.FileRw;
import com.king.cms.common.SessionUtil;
import com.king.cms.db.DBOperator;
import com.king.cms.db.QueryPage;

public class NewsAdminAction extends BaseDispatchAction{
	private static final Log  log = LogFactory.getLog(NewsAdminAction.class);
	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws java.lang.InstantiationException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		 String action = request.getParameter("action");
		 String module= request.getParameter("module");
		 if("add".equals(action)){
			 return mapping.findForward("newsedit");
		 }
		 if("modify".equals(action)){
			 String newsId = request.getParameter("newsId");
			  
			 String content = "";
			 String sql = "select new.*,user.USER_NAME from cms_touch_news new left join cms_user user on new.PUBLISH_USER = user.ID where new.ID=?";
			 List news = this.getDBOperator(request).select(sql, new Object[]{newsId});
			 if(news.size() != 0) {
				 Map m = (Map)news.get(0);
				 FileRw f = new FileRw();
//				 if(m.get("HTML_CONTENT") != null)
//					 content = new String((byte[])(m.get("HTML_CONTENT")));
				 String filePath = request.getRealPath("/") + "touch\\html\\" + m.get("NEWS_URL");
				  content =f.ReadFile(filePath);
				 request.setAttribute("data", m);
			 }
			 request.setAttribute("content", content);
			 return mapping.findForward("newsedit");
		 }
		 if("save".equals(action)){
			 String newsId = request.getParameter("newsId");
			 String sql = "";
			 String newsTitle = request.getParameter("newsTitle");
			 String content = request.getParameter("content");
			 DBOperator dbo = this.getDBOperator(request);
			 
			 String insertId = "";
			 if(null == newsId || "null".equals(newsId) || "".equals(newsId)){
				 sql = "insert into cms_touch_news (NEWS_TITLE,PUBLISH_DATE,PUBLISH_USER) values(?,?,?) ";
				 Integer uid = SessionUtil.getUserId(request);
				 log.info("teitle:" + newsTitle);
				 dbo.update(sql, new Object[]{newsTitle,new Date(),uid});
				 List lastIn = dbo.select("select  last_insert_id() as ID", null);
				 log.info(lastIn);
				 if(lastIn.size() > 0){
					 Map mm = (Map)lastIn.get(0);
					 insertId = mm.get("ID")+"";
				 }
				
				 dbo.update("update cms_touch_news set NEWS_URL=concat(concat('NEWS_',?),'.html') where ID=?", new Object[]{insertId,insertId});
			 }
			 else{
				 insertId = newsId; 
				 sql = "update cms_touch_news set NEWS_TITLE=?,PUBLISH_DATE=? where ID=?";
				 dbo.update(sql, new Object[]{newsTitle,new Date(),newsId});
			 
			 } 
			 FileRw f = new FileRw();
			 String realPath = request.getRealPath("/") + "touch\\html\\NEWS_" + insertId + ".html";
			 log.info("static html path:" + realPath);
			 f.writefilewithmqhxhtm(content, realPath);
			 return new ActionForward("/news.do?action=list");
		 }
		 if("del".equals(action)){
			 String[] newsIds = request.getParameterValues("newsId");
			 if(null != newsIds && newsIds.length > 0){
				 for(int i = 0;i < newsIds.length;i++){
					 String newsId = newsIds[i];
					 this.getDBOperator(request).update("delete from cms_touch_news where ID=?", new Object[]{newsId});
				 }
			 }
			 return new ActionForward("/news.do?action=list");
		 }
		 if("list".equals(action)){
			 String sql = "select new.*,user.USER_NAME from cms_touch_news new left join cms_user user on new.PUBLISH_USER = user.ID order by PUBLISH_DATE desc";
			 QueryPage query = new QueryPage(request, request.getContextPath()+"/tadmin/news.do" , sql);
			 request.setAttribute("data", query); 
			 return mapping.findForward("newslist");
		 }
		 
		 return null;
	 
	}
}
